﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_Fin_Fin003a : AdminBasePage
{
    decimal shipID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }

        if (!IsPostBack)
        {
            switch (ActionType)
            {

                case OperationType.Add:
                    btnSave.Text = "新增";
                    drpFrom.SelectedValue = LoginUserInfo.CurrentUser.ThirdUnitID.ToString();
                    break;
                case OperationType.Update:
                    btnSave.Text = "修改";
                    FinShipping fsp = new FinShipping();
                    fsp.LoadByPrimaryKey(shipID);

                    //txtSupplierNo.Text = fsp.SupplierBoxNo;
                    drpFrom.SelectedValue = fsp.FromDestination.ToString();
                    drpTo.SelectedValue = fsp.ToDestination.ToString();

                    txtComments.Text = fsp.Comments;
                    if (fsp.ShippingStatus != ConstantValue.ShippingStatusCode.Pending)
                    {
                        btnSave.Enabled = false;
                    }

                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;
            }
        }


    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        try
        {
            FinShipping fsp = new FinShipping();

            if (ActionType != OperationType.Delete)
            {
                if (string.IsNullOrEmpty(drpTransferType.SelectedValue))
                {
                    lbMessage.Text = "转移类型不能为空.";
                    return;
                }

                if (string.IsNullOrEmpty(drpFrom.SelectedValue))
                {
                    lbMessage.Text = "转出机构不能为空.";
                    return;
                }

                if (string.IsNullOrEmpty(drpTo.SelectedValue))
                {
                    lbMessage.Text = "转入机构不能为空.";
                    return;
                }
                
                if (drpFrom.SelectedValue == drpTo.SelectedValue)
                {
                    lbMessage.Text = "转出,转入机构不能相同.";
                    return;
                }
                //if (string.IsNullOrEmpty(txtSupplierNo.Text))
                //{
                //    fsp.Query.Where(fsp.Query.SupplierBoxNo == txtSupplierNo.Text.Trim());
                //    if (ActionType == OperationType.Update)
                //    {
                //        fsp.Query.Where(fsp.Query.ShipID != shipID);
                //    }

                //    fsp.Query.Load();

                //    if (fsp.es.HasData)
                //    {
                //        lbMessage.Text = "[Supplier No.]已使用,请输入新的[Supplier No.].";
                //        return;
                //    }
                //}
            }

            switch (ActionType)
            {

                case OperationType.Add:
                    fsp.FromDestination = decimal.Parse(drpFrom.SelectedValue);
                    fsp.ToDestination = decimal.Parse(drpTo.SelectedValue);
                    fsp.Comments = txtComments.Text;
                    fsp.ShippingStatus = ConstantValue.ShippingStatusCode.New;
                    //fsp.SupplierBoxNo = txtSupplierNo.Text;
                    fsp.CreatedBy = fsp.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    fsp.CreatedDttm = fsp.CreatedDttm = DateTime.Now;
                    fsp.Save();
                    break;
                case OperationType.Update:
                    fsp.LoadByPrimaryKey(shipID);
                    fsp.FromDestination = decimal.Parse(drpFrom.SelectedValue);
                    fsp.ToDestination = decimal.Parse(drpTo.SelectedValue);
                    fsp.Comments = txtComments.Text;
                    //fsp.SupplierBoxNo = txtSupplierNo.Text;
                    fsp.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    fsp.CreatedDttm = DateTime.Now;
                    fsp.Save();

                    break;
                case OperationType.Delete:
                    fsp.LoadByPrimaryKey(shipID);

                    try
                    {
                        using (esTransactionScope tr = new esTransactionScope())
                        {
                            if (fsp.ShippingStatus != ConstantValue.ShippingStatusCode.Pending)
                            {
                                WebHelper.GotoParent("无法删除状态为[转移中]记录");
                            }
                            else
                            {
                                FinShippingBoxesCollection sboxes = new FinShippingBoxesCollection();
                                sboxes.Query.Where(sboxes.Query.ShipID == shipID);
                                sboxes.Query.Load();

                                if (sboxes.Count > 0)
                                {
                                    var  boxIds = sboxes.Select(r=>r.BoxID.Value).ToArray();
                                    FinBoxCollection boxes = new FinBoxCollection();
                                    boxes.Query.Where(boxes.Query.BoxID.In(boxIds));
                                    boxes.Query.Load();

                                    foreach (var box in boxes)
                                    {
                                        box.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                                        box.SupplierBoxNo = "";
                                    }
                                    boxes.Save();

                                    sboxes.MarkAllAsDeleted();
                                    sboxes.Save();
                                }

                                fsp.MarkAsDeleted();
                                fsp.Save();


                            }
                            tr.Complete();
                        }
                        WebHelper.GotoParent();
                    }
                    catch (Exception ex)
                    {
                        WebHelper.GotoParent(ex.Message);

                    }
                    break;
            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

    protected void drpTransferType_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (drpTransferType.SelectedValue)
        {
            case "ZYLX001":
                initDropdown(drpFrom, "JGLX001");
                initDropdown(drpTo, "JGLX002,JGLX003");
                //drpFrom.ThirdType = "JGLX001";
                //drpTo.ThirdType = "JGLX002,JGLX003";
                break;
            case "ZYLX002":
                initDropdown(drpFrom, "JGLX001,JGLX002");
                initDropdown(drpTo, "JGLX001,JGLX002");
                //drpFrom.ThirdType = "JGLX001,JGLX002";
                //drpTo.ThirdType = "JGLX001,JGLX002";
                break;
            case "ZYLX003":
                initDropdown(drpFrom, "JGLX001,JGLX002");
                initDropdown(drpTo, "JGLX003");
                //drpFrom.ThirdType = "JGLX001,JGLX002";
                //drpTo.ThirdType = "JGLX003";
                break;
            default:
                break;
        }
    }

    private void initDropdown(DropDownList drp, string ThirdType)
    {
        BFThirdUnitCollection tunit = new BFThirdUnitCollection();
        ArrayList arg = new ArrayList();
        ArrayList argOr = new ArrayList();

        arg.Add(tunit.Query.Status == (int)ActiveStatus.Active);
        if (!string.IsNullOrEmpty(ThirdType))
        {
            string[] types = ThirdType.Split(',');
            if (types.Length == 1)
            {
                arg.Add(tunit.Query.ThirdType == types[0]);
            }
            else
            {
                if (types.Length > 1)
                {
                    for (int i = 0; i < types.Length; i++)
                    {
                        argOr.Add(tunit.Query.ThirdType == types[i]);
                    }
                    arg.Add(tunit.Query.Or(argOr.ToArray()));
                }
            }
        }

        if (arg.Count > 0)
        {
            tunit.Query.Where(arg.ToArray());
        }
        tunit.Query.Load();
        drp.DataSource = tunit;
        drp.DataTextField = BFThirdUnitMetadata.ColumnNames.Name;
        drp.DataValueField = BFThirdUnitMetadata.ColumnNames.ThirdUnitID;
        drp.DataBind();

        drp.Items.Insert(0, new ListItem("请选择...", ""));
    }
}